﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SnippetDoc.Utils;
using SnippetDoc.Entities;
using System.IO;
using System.Xml.Linq;

namespace SnippetDoc.UnitTests.Utils
{
    [TestClass]
    public class SnippetFactoryTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize()]
        public void SetUp()
        {
            
        }

        [TestCleanup()]
        public void TearDown()
        {
            
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestCreateSnippet_NullXDocument_ThrowException()
        {
            new SnippetFactory().CreateSnippet(null);
        }

        [TestMethod]
        [DeploymentItem(@"SnippetSamples\comm.snippet")]
        public void TestCreateSnippet_ValidSnippetSource_ASnippetObject()
        {
            //Arrange
            string snippetTitle = "comm";
            string snippetAuthor = "Juri Strumpflohner";
            string snippetDescription = "Create a new comment with signature";
            string snippetShortcut = "comm";

            //Act
            IEnumerable<Snippet> snippets = new SnippetFactory().CreateSnippet(XDocument.Load("comm.snippet"));

            //Assert
            Assert.IsNotNull(snippets, "The created snippets shouldn't be null");
            Assert.AreEqual(1, snippets.Count(), "There should be 1 snippet");

            Snippet snippet = snippets.ElementAt(0);

            Assert.AreEqual(snippetTitle, snippet.Title, "The title should match");
            Assert.AreEqual(snippetAuthor, snippet.Author, "The snippet author should match");
            Assert.AreEqual(snippetDescription, snippet.Description, "The snippet description");
            Assert.AreEqual(snippetShortcut, snippet.Shortcut, "The snippet shortcut");
        }

        [TestMethod]
        [DeploymentItem(@"SnippetSamples\comm.snippet")]
        public void TestCreateSnippet_ValidCodeSample_CodeSampleShouldBeLoaded()
        {
            //Arrange
            string codeSnippetSample = "//JS (day/month/year): comment";

            //Act
            IEnumerable<Snippet> snippets = new SnippetFactory().CreateSnippet(XDocument.Load("comm.snippet"));

            //Assert
            Assert.AreEqual(codeSnippetSample, snippets.ElementAt(0).CodeSample, "The code sample should match");
        }


    }
}
